﻿using System;
using System.Windows.Input;

namespace MBNet6;



/// <summary>
/// 基本用法:
/// 1.public VMCommand CommandName{get;set;}
/// 2.new VMCommand(...)
/// </summary>
public class VMCommand : ICommand
{
    public event EventHandler? CanExecuteChanged;
    private Action<object?> ActionExecute { get; set; }
    private Predicate<object?> PredicateCanExecute { get; set; }
    public VMCommand(Action<object?> execute)
    {
        ActionExecute = execute;
        PredicateCanExecute = DefaultCanExecute;
    }
    public VMCommand(Action<object?> execute, Predicate<object?> canExecute)
    {
        ActionExecute = execute;
        PredicateCanExecute = canExecute;
        CanExecuteChanged?.Invoke(this, new EventArgs());
    }


    #region 接口
    public bool CanExecute(object? parameter) => PredicateCanExecute(parameter);
    public void Execute(object? parameter) => ActionExecute(parameter);
    #endregion
    private bool DefaultCanExecute(object? parameter) => true;


}
